using Godot;
using System;

public partial class Node2d : Node2D
{
	public Vector2 daxiao,kaishidian,jieshudian;
	public bool isMouseDown = false;
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
	{
        GetNode<ColorRect>("ColorRect").Visible =false; // 初始时隐藏ColorRect

    }

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		var dian = kaishidian - jieshudian;
        


            if (dian.X < 0 && dian.Y < 0)
            {
                GetNode<ColorRect>("ColorRect").Position = kaishidian;
            }
            else if (dian.X > 0 && dian.Y > 0) { GetNode<ColorRect>("ColorRect").Position = jieshudian; }
            else if (dian.X < 0 && dian.Y > 0) { GetNode<ColorRect>("ColorRect").Position = new Vector2(kaishidian.X, jieshudian.Y); }
            else if (dian.X > 0 && dian.Y < 0) { GetNode<ColorRect>("ColorRect").Position = new Vector2(jieshudian.X, kaishidian.Y); }
            GetNode<ColorRect>("ColorRect").Size = new Vector2(Mathf.Abs(kaishidian.X - jieshudian.X), (Mathf.Abs(kaishidian.Y - jieshudian.Y)));

     // 如果鼠标未按下，则不执行后续代码
           //GetNode<ColorRect>("ColorRect").Size = new Vector2(Mathf.Max(kaishidian.X, jieshudian.X) - Mathf.Min(kaishidian.X, jieshudian.X), (Mathf.Max(kaishidian.Y, jieshudian.Y) - Mathf.Min(kaishidian.Y, jieshudian.Y)))
        
    }
    public override void _Input(InputEvent @event)
	{
		if (@event is InputEventMouseButton mouseButtonEvent)
		{
			if (mouseButtonEvent.Pressed)
			{
				isMouseDown = true;
                GD.Print("Mouse button pressed at: ", mouseButtonEvent.Position);
                kaishidian = mouseButtonEvent.Position;
            }
			else
			{
				isMouseDown = false;
                GetNode<ColorRect>("ColorRect").Visible = false; // 鼠标释放时重置拖动状态
                kaishidian = Vector2.Zero; // 重置起始点
                jieshudian = Vector2.Zero; // 重置结束点
                GD.Print("Mouse button released at: ", mouseButtonEvent.Position);
			}
			
		}
        if (isMouseDown && @event is InputEventMouseMotion mouseMotion)
        {
            GetNode<ColorRect>("ColorRect").Visible = true;
            jieshudian = mouseMotion.Position;
            GD.Print(kaishidian.X - jieshudian.X, kaishidian.Y - jieshudian.Y);
        }
     

    }
}
